# ================================ celery ================================

from rest_framework.views import APIView
from rest_framework.response import Response
# celery使用测试
from mycerlery.sms.tasks import send_sms, send_xxx

class Adduser(APIView):
    def get(self,request):
        # UserModel.objects.create_user(username='root',email='123#123.com',password='123',money=0)

        # 执行异步任务
        # send_sms.delay()
        # 定时执行异步任务
        ret3 = send_xxx.apply_async(countdown=15)

        print('视图函数被执行.....')
        return Response({'message':"sfdadfad"})
    
# ================================ ronglianyun ================================

from dingxiang.utils.ronglianyunapi import send_message
from django.conf import settings
import random
from django_redis import get_redis_connection
from user.ser import CoustomTokenObtainPairSerializer
from mycerlery.sms.tasks import send_sms

class SmsAPIView(APIView):
    def get(self, request, mobile):
        # http://shuqiwang.com/book/10394/
        # 调用发送短信工具类-----发送短信

        redis = get_redis_connection('sms_code')

        # ttl 判断key是否过期   -2
        # 刚进入发送验证码视图,需要判断,在生成验证码的地方往redis存的过期时间
        # redis.setex(f"interval_{mobile}", sem_interval, '_')
        res_interval = redis.ttl(f"interval_{mobile}")
        if res_interval != -2:
            return Response({'message': '验证码还没过期', 'status':'400'})

        ronglianyun = settings.RONGLIANYUN

        # 短信模板id
        tid = ronglianyun.get('reg_tid')
        # 短信过期时间
        sms_expire = ronglianyun.get('sms_expire')
        # 短信发送间隔、冷却时间
        sem_interval = ronglianyun.get('sem_interval')

        # 保存时间间隔
        interval = sem_interval/60
        # 4位数随机验证码 长度不足四位用0填充 将生成的随机校验码添加redis中
        # 格式化指令
        code = f"{random.randint(0,9999):04d}"
        # 保存短信验证码
        datas = (code, interval)
        print('生成随机验证码.....')
        print(code)
        # 验证码
        # 过期时间

        # 异步任务
        send_sms.delay(tid, mobile, datas)

        # 发送短信验证码
        # res = send_message(tid, mobile, datas)

        # if res:
        #     # 短信发送成功,往redis保存信息

        redis.setex(f"sms_{mobile}", sms_expire, code)
        redis.setex(f"interval_{mobile}", sem_interval, '_')

        return Response({'message': '发送成功', 'status':'200'})

# ================================ oss ================================

import oss2,uuid
if __name__ == '__main__':
    # OSS 认证实例
    OSS_ACCESS_KEY_ID = "LTAI5tHCUwPPiKDGL29Bd4MB"
    OSS_ACCESS_KEY_SECRET = "q5fY1siKCUgenmgfCf9snNBulW6Pwp"
    OSS_ENDPOINT = "oss-cn-beijing.aliyuncs.com"  # 访问域名, 根据服务器上的实际配置修改
    OSS_BUCKET_NAME = "zcxapi"  # oss 创建的 BUCKET 名称

    # 存储桶
    OSS_SERVER_URL = f"https://{OSS_BUCKET_NAME}.{OSS_ENDPOINT}"

    # 创建命名空间操作实例对象
    # 创建认证实例
    auth = oss2.Auth(OSS_ACCESS_KEY_ID, OSS_ACCESS_KEY_SECRET)
    # 获取存储空间
    bucket = oss2.Bucket(auth, OSS_ENDPOINT, OSS_BUCKET_NAME)

    # 上传文件
    image = f"demo/test.jpg"
    file_path = 'D:\\jiyun\\H2406A\\dingxiang\\dingxiang\\media\\avatar\\2024\\avatar.jpg'
    with open(file_path, "rb") as f:
        # 生成唯一的文件名
        unique_filename = f"{uuid.uuid4()}.{image.split('.')[-1]}"
        result = bucket.put_object(image, f.read() )

        print(result)
        print(result.status)
        print(f"文件上传成功,URL: {OSS_SERVER_URL}/{image}")
